<!DOCTYPE html>
<html lang="en" class="no-js" xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout" layout:decorator="layout">
<head>
    <title>搭配套餐管理</title>
</head>
<body>

<div layout:fragment="content">

    <div class="row">
        <div class="col-md-12">
            <div class="btn-group pull-right">
                <a href="index.html" th:href="@{'/promotion/combo/index?page='+${page}+'&amp;viewState='+${viewState}}" id="link_cancel" class="btn green">返回<i class="fa fa-arrow-circle-o-left"></i></a>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-12">
            <div class="form">
                <form role="form" id="form_combo_edit" th:action="@{'/promotion/combo/save?page='}+${page}+'&amp;viewState='+${viewState}" th:object="${combo}" method="post" class="form-horizontal">
                    <h4 class="form-section">搭配套餐信息</h4>
                    <input id="hidden_id" type="hidden" th:field="*{id}" />
                    <div class="form-body">
                        <div class="form-group" th:class="${#fields.hasErrors('name')}? 'form-group has-error' : 'form-group'">
                            <label class="control-label col-md-3">套餐名称<span class="required" aria-required="true">* </span></label>
                            <div class="col-md-4">
                                <input type="text" id="name" required="" name="name" class="form-control" placeholder="套餐名称" th:field="*{name}"/>
                                <span class="help-block" th:if="${#fields.hasErrors('name')}" th:errors="*{name}">套餐名称不能为空</span>
                            </div>
                        </div>

                        <div class="form-group" th:class="${#fields.hasErrors('startDate')}? 'form-group has-error' : 'form-group'">
                            <label class="control-label col-md-3">开始时间<span class="required" aria-required="true">* </span></label>
                            <div class="col-md-4">
                                <input id="text_startDate" th:field="*{startDate}" class="form-control" type="text"/>
                                <span class="help-block" th:if="${#fields.hasErrors('startDate')}" th:errors="*{startDate}">请输入开始时间</span>
                            </div>
                        </div>

                        <div class="form-group" th:class="${#fields.hasErrors('endDate')}? 'form-group has-error' : 'form-group'">
                            <label class="control-label col-md-3">结束时间<span class="required" aria-required="true">* </span></label>
                            <div class="col-md-4">
                                <input id="text_endDate" th:field="*{endDate}" data-format="yyyy-MM-dd hh:mm:ss" class="form-control" type="text"/>
                                <span class="help-block" th:if="${#fields.hasErrors('endDate')}" th:errors="*{endDate}">请输入结束时间</span>
                            </div>
                        </div>

                        <div class="form-group"  th:class="${#fields.hasErrors('price')}? 'form-group has-error' : 'form-group'">
                            <label class="control-label col-md-3">套餐价格<span class="required" aria-required="true">* </span></label>
                            <div class="col-md-4">
                                <input type="text" required="" id="text_price" th:field="*{price}" class="form-control"/>
                                <span class="help-block" th:if="${#fields.hasErrors('price')}" th:errors="*{endDate}">请输入套餐价格</span>
                            </div>
                        </div>

                        <div class="form-group" th:class="${#fields.hasErrors('displayOrder')}? 'form-group has-error' : 'form-group'">
                            <label class="control-label col-md-3">排序<span class="required" aria-required="true">* </span></label>
                            <div class="col-md-7">
                                <input type="text" required="" id="displayOrder" name="displayOrder" class="form-control" placeholder="排序" th:field="*{displayOrder}" />
                                <span class="help-block" th:if="${#fields.hasErrors('displayOrder')}" th:errors="*{displayOrder}">请输入排序</span>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="control-label col-md-3">描述</label>
                            <div class="col-md-4">
                                <textarea id="textarea_description" rows="4" type="text" class="form-control" placeholder="描述" th:field="*{description}" />
                            </div>
                        </div>
                        <div class="form-actions fluid" style="display: none;">
                            <div class="col-md-offset-3 col-md-9">
                                <button id="btn_form_submit" type="submit" class="btn blue">提交</button>
                            </div>
                        </div>
                        <input type="hidden" th:field="*{comboGoodsIds}" id="comboGoodsIds" />
                    </div>
                </form>
            </div>
        </div>
        <div class="row" th:with="selectedGoods=${combo.comboGoods},selectableGoods=${selectableGoods}">
            <div style="padding-left: 10px;" class="col-md-12">
                <div th:replace="support/searchGoods :: search_form"></div>
            </div>
            <div class="col-md-12">
                <div class="form">
                    <div class="form-body">
                        <div th:replace="support/searchGoods :: search_list"></div>
                    </div>
                </div>
            </div>
        </div>
        <div style="margin-top: 10px;" class="form-actions fluid">
            <div class="col-md-offset-3 col-md-5">
                <button id="bt_submit" onclick="checkAndSubmit()" class="btn blue btn-block">提交</button>
            </div>
        </div>
    </div>

</div>
</body>
</html>

<script th:inline="javascript">
    /*<![CDATA[*/

    function checkAndSubmit(){
        var startTime=$("#text_startDate").val();
        var start=new Date(startTime.replace("-", "/").replace("-", "/"));
        var endTime=$("#text_endDate").val();
        var end=new Date(endTime.replace("-", "/").replace("-", "/"));
        if(end <= start){
            alert("开始时间要小于结束时间");
            return  false;
        }
        $('#comboGoodsIds').val(getSelectedGoodsIds());
        if(!$('#comboGoodsIds').val()){
            alert("选择套餐的商品");
            return  false;
        }
        $('#btn_form_submit').click();
    }

    $("#text_startDate").click(function(){
        WdatePicker({startDate:'%y-%M-01 00:00:00',dateFmt:'yyyy-MM-dd HH:mm:ss',alwaysUseStartDate:true,readOnly:true});
    }).focus(function () {
        WdatePicker({startDate:'%y-%M-01 00:00:00',dateFmt:'yyyy-MM-dd HH:mm:ss',alwaysUseStartDate:true,readOnly:true});
    });
    $("#text_endDate").click(function(){
        WdatePicker({startDate:'%y-%M-01 00:00:00',dateFmt:'yyyy-MM-dd HH:mm:ss',alwaysUseStartDate:true,readOnly:true});
    }).focus(function () {
        WdatePicker({startDate:'%y-%M-01 00:00:00',dateFmt:'yyyy-MM-dd HH:mm:ss',alwaysUseStartDate:true,readOnly:true});
    });
    $.validator.addMethod('decimalScal',function(value,element){
        if(value.split('.').length>2){
            return false;
        }
        if(value.indexOf('.') != -1){
            var length = value.slice(value.indexOf('.')).length;
            if(length>3){
                return false;
            }else{
                return true;
            }
        }
        return true;
    },'运费尾数最多两位');


    $(document).ready(function(){
    	if($.browser.mozilla){
			Metronic.alert({'message':'<span style="color:red;">请使用谷歌或者IE浏览器进行多文件的上传，火狐浏览器暂不支持!</span>'});
		}
        $("#form_combo_edit").validate({
            rules:{
                name:{
                    required:true,
                    maxlength:100
                },
                displayOrder:{
                    required:true,
                    min:0,
                    max:254
                },
                price:{
                    min:0,
                    max:99999999.99,
                    decimalScal:true
                },
                startDate:{
                    required:true
                },
                endDate:{
                    required:true
                }
            },
            messages:{
                name:{
                    required:'请输入套餐名称',
                    maxlength:"套餐名称不可超过100"
                },
                displayOrder:{
                    required:'请输入套餐排序',
                    min:'必须为整数且取值范围0-254',
                    max:'必须为整数且取值范围0-254'
                },
                price:{
                    required:'请输入套餐价格',
                    min:'价格最小值为0且必须为数字',
                    max:'价格最大值为99999999.99且必须为数字',
                    decimalScal:'价格尾数最多两位'
                },
                startDate:{
                    required:'请输入套餐开始时间'
                },
                endDate:{
                    required:'请输入套餐结束时间'
                }
            }
        });
    });
    KindEditor.ready(function(K) {
        var editor = K.create('#textarea_description', {
            uploadJson : '/upload/COMBO',
            allowFileManager : false
        });
        editor.clickToolbar('multiimage',function(){
	    	   if($.browser.mozilla){
	    		   editor.hideDialog();
	    		   alert('请使用谷歌或者IE浏览器进行多文件的上传，火狐浏览器暂不支持!');
	    		   return false;
	    	   }
	       });
    });
    /*]]>*/
</script>
